/*
 * Project: greenline-biz-service
 * 
 * File Created at 2012-3-21
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */

package com.greenline.push.service.core.utils.mail.impl;

import java.text.MessageFormat;
import java.util.Map;

import org.apache.log4j.Logger;

import com.greenline.common.util.RegexUtil;
import com.greenline.message.biz.service.share.mail.MailParameterDTO;
import com.greenline.message.biz.service.share.mail.MailResultDTO;
import com.greenline.message.biz.service.share.mail.MailService;
import com.greenline.push.service.core.utils.Constants;

/**
 * 
 * @Type MailServiceImpl
 * @Desc 邮件服务
 * @author weirui.shenwr
 * @date 2012-4-26
 * @Version V1.0
 */
public class MailServiceImpl implements com.greenline.push.service.core.utils.mail.MailService {

    private static final Logger logger = Constants.LOG_PROCESS;

    private MailService hessianTextMailService;
    private MailService hessianHtmlMailService;
    private String appid;

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public MailService getHessianTextMailService() {
        return hessianTextMailService;
    }

    public void setHessianTextMailService(MailService hessianTextMailService) {
        this.hessianTextMailService = hessianTextMailService;
    }

    public MailService getHessianHtmlMailService() {
        return hessianHtmlMailService;
    }

    public void setHessianHtmlMailService(MailService hessianHtmlMailService) {
        this.hessianHtmlMailService = hessianHtmlMailService;
    }

    @Override
    public boolean send(String templateName, String to, Map<String, String> data, boolean textmail) {
        return send(templateName, new String[] { to }, data, textmail);
    }

    @Override
    public boolean send(String templateName, String[] to, Map<String, String> data, boolean textmail) {
        MailService mailService = hessianTextMailService;
        if (!textmail) {
            mailService = hessianHtmlMailService;
        }
        return sendMail(templateName, to, data, mailService);
    }

    /**
     * 调用hessia 邮件服务发邮件
     * 
     * @param templateName
     * @param to
     * @param data
     * @param mailservice
     * @return
     */
    private boolean sendMail(String templateName, String[] to, Map<String, String> data, MailService mailservice) {
        if (to == null || to.length == 0) {
            logger.error(MessageFormat.format("mail.send.error templateName={0} to={1} message={2}", templateName, to, "sendMail：to为空"));
            return false;
        }
        for (int i = 0; i < to.length; i++) {
            if (!RegexUtil.isEmail(to[i])) {
                logger.error(MessageFormat.format("mail.send.error templateName={0} to={1} message={2}", templateName, to,
                        "sendMail：邮箱格式错误" + to[i]));
                return false;
            }
        }
        // MailParamDTO param = new MailParamDTO();
        // param.setAppid(this.appid);
        // param.setTo(to);
        // param.setTemplateName(templateName);
        // param.setData(data);
        MailParameterDTO param = new MailParameterDTO();
        param.setAppid(this.appid);
        param.setTo(to);
        param.setTemplateName(templateName);
        param.setData(data);

        MailResultDTO result = mailservice.sendEMail(param);
        if (result.isSuccess()) {
            return true;
        } else {
            logger.error(MessageFormat.format("mail.send.error templateName={0} to={1} message={2}", templateName, to, result.getMessage()));
            return false;
        }
    }
}
